import Promise from '../../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../../utils/regenerator-runtime/runtime'
const utils = require('../../../../utils/util.js');
const app=getApp();
Page({
  data: {
    isHide:false,
    loadUrl:"/api/MemberTask/taskList", //加载url
    doCompleteUrl:"/api/SeniorService/doComplete",
    page:1,
    pagesize:6,
    list:null,
    token:null,
    condition:1,
    noMoreData:false,
    loadingText:"上拉加载更多",
    loadingTextArr:["上拉加载更多","加载中...","没有更多数据了"]
  },
  //事件处理函数
  jumpDetail: function(e) {
    var taskid=e.currentTarget.dataset.taskid;
    var status=e.currentTarget.dataset.status;
    var bankname=e.currentTarget.dataset.bankname;
    var type=e.currentTarget.dataset.type;
    var statusid=e.currentTarget.dataset.statusid;
    if(statusid==0) return;
    wx.navigateTo({
      url: '../detail/index?taskid='+taskid+'&status='+status+'&bankname='+bankname+'&type='+type,
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  doComplete: async function(e){
    var applyid=e.currentTarget.dataset.applyid;
    var result=await this._doCompleteData(applyid);
    if(result.state){
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      this.onLoad();
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
  },
  _doCompleteData:function(applyid){
    var p = new Promise((resolve,reject)=>{
      //做一些异步操作
      app.request(this.data.doCompleteUrl,{token:this.data.token,id:applyid},res=>{
        //console.log(res);
        var data=res.data;
        if(data.state){
          //resolve({state:true,list:JSON.parse(data.list)});
          resolve({state:true,msg:data.msg});
        }
        else{
          resolve({state:false,msg:data.msg});          
        }
      },error=>{
        resolve({state:false,msg:"刷新失败，请重试"});
      })
    });
    return p;
  },
  changeCondition: async function(e){
    var condition=e.currentTarget.dataset.condition;
    this.setData({
      condition:condition,
      page:1
    })
    var result=await this._loadData();
    if(result.state){
      var list=result.list;
      this.setData({
        list:list
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
  },
  onShow:function(){
    console.log("onShow");
    if(this.data.isHide) this.onLoad();
    this.setData({
      isHide:false
    })
  },
  onHide:function(){
    this.setData({
      isHide:true
    })
  },
  onLoad: async function () {
    console.log("onLoad");
    const token = await app.getToken();
    this.setData({
      token:token,
      loadingText:this.data.loadingTextArr[1],
      page:1
    })
    var result=await this._loadData();
    if(result.state){
      var list=result.list;
      this.setData({
        list:list,
        loadingText:list.length==0?this.data.loadingTextArr[2]:this.data.loadingTextArr[0],
        noMoreData:list.length==0
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      this.setData({
        loadingText:this.data.loadingTextArr[0]
      })
    }
  },
  _loadData:function(){
      var p = new Promise((resolve,reject)=>{
          //做一些异步操作
          app.request(this.data.loadUrl,{token:this.data.token,page:this.data.page,pagesize:this.data.pagesize,condition:this.data.condition},res=>{
            //console.log(res);
            var data=res.data;
            /* var data={
              state:true,
              "data": [
                {
                    "apply_id": 33,
                    "type": 1,
                    "status": 2,
                    "bankname": "招商银行",
                    "create_time": 1548776652,
                    "task_log": {
                        "task_id": 47,
                        "status": 2,
                        "create_time": "01-29 23:44",
                        "status_msg": "您的申请处理状态：已预约"
                    },
                    "apply_type": 1
                },
                {
                    "apply_id": 3,
                    "p_remark": "测试备注",
                    "create_time": 1548052207,
                    "s_list": [
                        {
                            "task_id": 5,
                            "bankname": "中国银行",
                            "remark": "21号计划第一条",
                            "status": 1,
                            "type": 1,
                            "status_cn": "可预约",
                            "show_remark": 1,
                            "task_log": [
                                {
                                    "status": 6,
                                    "create_time": "01-21 14:33",
                                    "status_msg": "您的申请处理状态：延期"
                                },
                                {
                                    "status": 1,
                                    "create_time": "01-21 14:33",
                                    "status_msg": "您的申请处理状态：可预约"
                                },
                                {
                                    "status": 5,
                                    "create_time": "01-21 14:33",
                                    "status_msg": "您的申请处理状态：审核失败"
                                }
                              ]
                        }
                      ]
                  }  
                ]    
            }; */
            if(data.state){
              //resolve({state:true,list:JSON.parse(data.list)});
              resolve({state:true,list:data.data});
            }
            else{
              resolve({state:false,msg:data.msg});          
            }
          },error=>{
            resolve({state:false,msg:"刷新失败，请重试"});
          })
      });
      return p;
  },
  onPullDownRefresh: async function () {
    this.setData({
      page:1
    })
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var result= await this._loadData();
    if(result.state){
      var list=result.list;
      this.setData({
        list:list,
        loadingText:list.length==0?this.data.loadingTextArr[2]:this.data.loadingTextArr[0],
        noMoreData:list.length==0
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      this.setData({
        loadingText:this.data.loadingTextArr[0]
      })
    }
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
  },
  onReachBottom:  async function () {
    if(this.data.noMoreData) return;
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    this.setData({
      page:this.data.page+1,
      loadingText:this.data.loadingTextArr[1]
    })
    var result=await this._loadData();
    if(result.state){
      var list=this.data.list.concat(result.list);
      this.setData({
        list:list,
        loadingText:result.list.length==0?this.data.loadingTextArr[2]:this.data.loadingTextArr[0],
        noMoreData:result.list.length==0
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      this.setData({
        loadingText:this.data.loadingTextArr[0]
      })
    }
    // 隐藏加载框
    wx.hideLoading();
  }
})
